package us.codecraft.webmagic.utils;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.Iterator;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;


public class MongoUtil {
	public static final String TimeStampKey = "time";
	public static final String PrimaryKey = "url";
	public static final long Days = 86400000;
	
	Mongo mc;
	DB db;
	DBCollection coll;
	Date date= new java.util.Date();
	
	public MongoUtil () throws UnknownHostException, MongoException {
		mc = new Mongo();
	}
	
	public MongoUtil (String DataBase) throws UnknownHostException, MongoException {
		mc = new Mongo();
		db = mc.getDB(DataBase);
	}
	
	public MongoUtil (String DataBase,String Collection) throws UnknownHostException, MongoException {
		mc = new Mongo();
		db = mc.getDB(DataBase);
		coll = db.getCollection(Collection);
	}
	
	public MongoUtil (Mongo mc,DB DataBase,DBCollection Collection) throws UnknownHostException, MongoException {
		this.mc = mc;
		this.db = DataBase;
		this.coll = Collection;
	}
	
	public MongoUtil (String Host,String DataBase,String Collection) throws UnknownHostException, MongoException {
		mc = new Mongo(Host);
		db = mc.getDB(DataBase);
		coll = db.getCollection(Collection);
	}
	
	public void setMongoClient (Mongo mc) {
		this.mc = mc;
	}
	
	public String FineOne (String FieldKey) {
		return coll.findOne().get(FieldKey).toString();
	}
	
	public DBObject FineOne () {
		return coll.findOne();
	}
	
	public Iterator<DBObject> Find (String FieldKey,String FieldValue) {
		return getCursor(FieldKey,FieldValue).iterator();
	}
	
	public boolean ValueExist (String FieldKey,String FieldValue) {
		return getCursor(FieldKey,FieldValue).hasNext();
	}
		
	//Get Cursor Position of The Found Field:Value.If Multiple,Return the First(As Mongodb Defined)
	public DBCursor getCursor (String FieldKey,String FieldValue) {
		BasicDBObject query = new BasicDBObject();
		query.put(FieldKey, FieldValue);
		
		DBCursor cursor = coll.find(query);
		return cursor;
	}
	
	public void MongoParseLinking(DBCollection htmlDoc,DBCollection linking) throws IOException {
		
        DBCursor mongoDoc = htmlDoc.find();
        BasicDBObject linkEntry = new BasicDBObject();
		
        while (mongoDoc.hasNext()){
        	String html = mongoDoc.next().get("html").toString();
    		String url =  mongoDoc.next().get("url_hash").toString();
    		
    		Document doc = Jsoup.parse(html);
    		Elements linksElements = doc.getElementsByTag("a");
    				 
    		BasicDBObject[] pageLinks = new BasicDBObject[linksElements.size()];
    		for (int i=0;i<linksElements.size();i++) {
//    			pageLinks[i] = linksElements.get(i).attr("href").concat("=").concat(linksElements.get(i).text());
    			linkEntry.append("link", linksElements.get(i).attr("href"));
    			linkEntry.append("anchor", linksElements.get(i).text());
    			pageLinks[i] = new BasicDBObject(linkEntry);
    		}
    		    		
    		BasicDBObject bdb = new BasicDBObject();
    		bdb.append("url",url);
    		bdb.append("linking_to", pageLinks);
    		bdb.append("time", date.getTime());
    		
    		BasicDBObject query = new BasicDBObject();
    		query.append("url", url);
    		DBObject targetDoc = linking.findOne(query);
    		
    		if (targetDoc == null){
    			linking.insert(bdb);
    		}else{
    			linking.update(targetDoc, bdb);
    		}
        }
        
	}
	
	public boolean Revisitable(BasicDBObject entry) {
		// TODO Auto-generated method stub
		long x = Long.valueOf(entry.get(TimeStampKey).toString());
		if ((date.getTime() - x) >  Days) {
			return true;
		}else{
			return false;
		}
	}
}
